{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1. 假设我们输入矩阵\n",
    "\n",
    "$\n",
    "M=\\left(\\begin{array}{c}\n",
    "1&-1&0\\\\\n",
    "-1&2&1\\\\\n",
    "0&2&-2\n",
    "\\end{array}\\right)\n",
    "$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Matrix shape is:  (3, 3, 1)\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "\n",
    "M = np.array([\n",
    "        [[1],[-1],[0]],\n",
    "        [[-1],[2],[1]],\n",
    "        [[0],[2],[-2]]\n",
    "    ])\n",
    "\n",
    "print(\"Matrix shape is: \",M.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2. 定义卷积过滤器, 深度为1。\n",
    "$\n",
    "W=\\left(\\begin{array}{c}\n",
    "1&-1\\\\\n",
    "0&2\n",
    "\\end{array}\\right)\n",
    "$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "filter_weight = tf.get_variable('weights', shape=[2, 2, 1, 1], initializer = tf.constant_initializer([\n",
    "                                                                        [1, -1],\n",
    "                                                                        [0, 2]]))\n",
    "biases = tf.get_variable('biases', shape=[1], initializer = tf.constant_initializer(1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3. 调整输入的格式符合TensorFlow的要求。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[[ 1.],\n",
       "         [-1.],\n",
       "         [ 0.]],\n",
       "\n",
       "        [[-1.],\n",
       "         [ 2.],\n",
       "         [ 1.]],\n",
       "\n",
       "        [[ 0.],\n",
       "         [ 2.],\n",
       "         [-2.]]]], dtype=float32)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "M = np.asarray(M, dtype='float32')\n",
    "M = M.reshape(1, 3, 3, 1)\n",
    "M"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 4.  计算矩阵通过卷积层过滤器和池化层过滤器计算后的结果。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "convoluted_M: \n",
      " [[[[ 7.]\n",
      "   [ 1.]]\n",
      "\n",
      "  [[-1.]\n",
      "   [-1.]]]]\n",
      "pooled_M: \n",
      " [[[[ 0.25]\n",
      "   [ 0.5 ]]\n",
      "\n",
      "  [[ 1.  ]\n",
      "   [-2.  ]]]]\n"
     ]
    }
   ],
   "source": [
    "x = tf.placeholder('float32', [1, None, None, 1])\n",
    "conv = tf.nn.conv2d(x, filter_weight, strides = [1, 2, 2, 1], padding = 'SAME')\n",
    "bias = tf.nn.bias_add(conv, biases)\n",
    "pool = tf.nn.avg_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')\n",
    "with tf.Session() as sess:\n",
    "    tf.global_variables_initializer().run()\n",
    "    convoluted_M = sess.run(bias,feed_dict={x:M})\n",
    "    pooled_M = sess.run(pool,feed_dict={x:M})\n",
    "    \n",
    "    print(\"convoluted_M: \\n\", convoluted_M)\n",
    "    print(\"pooled_M: \\n\", pooled_M)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
